﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coater.UC
{
    public partial class ucTrackBar : UserControl
    {
        private int _value;
        private int _minimum;
        private int _maximum;
        private bool _dragging;

        public event EventHandler ValueChanged;

        public ucTrackBar()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
            Minimum = 0;
            Maximum = 10;
            Value = 0;
        }

        private void OnValueChanged()
        {
            ValueChanged?.Invoke(this, EventArgs.Empty);
        }

        public int Value
        {
            get { return _value; }
            set { _value = value; this.Refresh(); }
        }

        public int Minimum
        {
            get { return _minimum; }
            set { _minimum = value; this.Refresh(); }
        }

        public int Maximum
        {
            get { return _maximum; }
            set { _maximum = value; this.Refresh(); }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                _dragging = true;
                UpdateValueFromPoint(e.Location);
                Capture = false; // Release capture to allow dragging the control if needed
                MouseMove += Control_MouseMove; // Hook mouse move event to update value during drag
                MouseUp += Control_MouseUp; // Hook mouse up event to stop dragging and cleanup
            }
        }

        private void Control_MouseMove(object sender, MouseEventArgs e)
        {
            if (_dragging) UpdateValueFromPoint(e.Location);
        }

        private void Control_MouseUp(object sender, MouseEventArgs e)
        {
            _dragging = false;
            MouseMove -= Control_MouseMove; // Cleanup event handlers when done dragging
            MouseUp -= Control_MouseUp; // Unhook mouse events to stop dragging and cleanup
        }

        private void UpdateValueFromPoint(Point p)
        {
            int newValue = (int)((p.X / (float)Width) * (Maximum - Minimum)) + Minimum;
            if (newValue != Value) // Only update if the value has changed to avoid flicker and unnecessary repaints
            {
                Value = newValue; // This will trigger the ValueChanged event if bound to a property or method.
                Invalidate(); // Force a repaint of the control to reflect the new value.
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e); // Call the base OnPaint to ensure standard control behavior is handled.
            DrawSlider(e.Graphics); // Custom drawing logic goes here. You can call this method in this override.
            OnValueChanged();
        }

        private void DrawSlider(Graphics g)
        {
            // Example drawing logic: draw the track and thumb (knob) of the slider. Adjust as needed.
            int thumbWidth = 20; // Width of the thumb (knob). Adjust as needed.
            int thumbHeight = Height - 4; // Height of the thumb (knob). Adjust as needed.
            int thumbX = (int)((Width - thumbWidth) * ((Value - Minimum) / (float)(Maximum - Minimum))); // Calculate thumb position based on value.
            Rectangle trackRect = new Rectangle(0, Height / 2 - thumbHeight / 2 + 5, Width, thumbHeight - 10); // Rectangle for the track. Adjust as needed.
            Rectangle thumbRect = new Rectangle(thumbX, Height / 2 - thumbHeight / 2, thumbWidth, thumbHeight); // Rectangle for the thumb (knob). Adjust as needed.
            g.FillRectangle(new SolidBrush(Color.FromArgb(231, 234, 234)), trackRect); // Draw the track. Adjust brush color as needed.
            g.FillRectangle(new SolidBrush(Color.FromArgb(0, 122, 217)), thumbRect); // Draw the thumb (knob). Adjust brush color as needed.
        }

    }
}
